package work;

class work1 {
    public int j = 0;
}

class addTest implements Runnable{
    private work1 w;

    public addTest(work1 w) {
        this.w = w;
    }

    @Override
    public void run() {
        while (true){
            synchronized (w) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                add();
            }
        }
    }

    public void add(){
        w.j++;
        System.out.println(Thread.currentThread().getName() + w.j);
    }
}

class cutTest implements Runnable{
    private work1 w;

    public cutTest(work1 w) {
        this.w = w;
    }

    @Override
    public void run() {
        while (true){
            synchronized (w) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                cut();
            }
        }
    }

    public void cut(){
        w.j--;
        System.out.println(Thread.currentThread().getName() + w.j);
    }
}

class TestWork1{
    public static void main(String[] args) {
        work1 w1 = new work1();
        for (int i = 1; i <= 2; i++) {
            new Thread(new addTest(w1),"第" + i + "个加法").start();
            new Thread(new cutTest(w1),"第" + i + "个减法").start();
        }
    }
}